/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.executor.mpp.deploy;

import com.google.inject.Provides;
import com.alibaba.polardbx.common.properties.MppConfig;
import com.alibaba.polardbx.executor.mpp.Threads;
import com.alibaba.polardbx.executor.mpp.execution.ForDriverYieldExecutor;
import com.alibaba.polardbx.executor.mpp.execution.ForQueryExecution;
import com.alibaba.polardbx.executor.mpp.execution.ForTaskNotificationExecutor;
import io.airlift.configuration.AbstractConfigurationAwareModule;

import javax.inject.Singleton;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

import static java.util.concurrent.Executors.newFixedThreadPool;
import static java.util.concurrent.Executors.newScheduledThreadPool;

public abstract class BaseModule extends AbstractConfigurationAwareModule {

    @Provides
    @Singleton
    @ForQueryExecution
    public static ExecutorService createQueryExecutionExecutor() {
        int poolSize = MppConfig.getInstance().getQueryExecutionThreadPoolSize();
        return newFixedThreadPool(poolSize, Threads.daemonThreadsNamed("query-execution"));
    }

    @Provides
    @Singleton
    @ForTaskNotificationExecutor
    public static ExecutorService createTaskNotificationExecutor() {
        return newFixedThreadPool(MppConfig.getInstance().getTaskNotificationThreads(),
            Threads.tpThreadsNamed("task-notification"));
    }

    @Provides
    @Singleton
    @ForDriverYieldExecutor
    public static ScheduledExecutorService createDriverYieldExecutor() {
        return newScheduledThreadPool(MppConfig.getInstance().getTaskYieldThreads(),
            Threads.tpThreadsNamed("task-yield"));
    }
}
